﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Common.CommandTrees;
using System.Data.Entity;
using System.Linq;
using System.Web;
using WebTV_Kickstarter;

namespace WebTV_Kickstarter
{
    public  partial class Message
    {
        
        private static EFDataModelContainer db = new EFDataModelContainer();
        public static List<Message> Index()
        {
            return db.MessageSet.ToList();
        }

        public static void AddMessage(Message message)
        {
            db.MessageSet.Add(message);
            message.Project = db.ProjectSet.Find(message.ProjectId);
            message.Gebruiker = db.GebruikerSet.Find(message.GebruikerId);
            message.Project.NumberOfMessages++;    
            db.SaveChanges();
        }

        public static void AddMessage(Message message, EFDataModelContainer eFDataModelContainer)
        {
            eFDataModelContainer.MessageSet.Add(message);
            message.Project = eFDataModelContainer.ProjectSet.Find(message.ProjectId);
            message.Gebruiker = eFDataModelContainer.GebruikerSet.Find(message.GebruikerId);
            message.Project.NumberOfMessages++;
            eFDataModelContainer.SaveChanges();
        }

        public static Message FindMessage(int? id)
        {
            return db.MessageSet.Find(id);
        }

        public static DbSet GetGebruikerSet()
        {
            return db.GebruikerSet;
        }
        
        public static DbSet GetProjectSet()
        {
            return db.ProjectSet;
        }

        public static List<Message> FindMessages(int? id)
        {
            List<Message> messageset = db.MessageSet.Where(m => m.ProjectId == id).ToList();
            return messageset;
        }

        public static Message GetMessageByUser(int userId, int? id)
        {
            return db.MessageSet.Where(m => m.GebruikerId == userId && m.ProjectId == id).FirstOrDefault();
        }

        internal static Message[] getMessageFromUser(int id)
        {
            EFDataModelContainer db = new EFDataModelContainer();
            Gebruiker g = Gebruiker.FindGebruiker(id);
            ICollection<Message> message = g.Message;
            Message[] messageArray =
            (
                from m in message
                where m.Gebruiker == g
                select m
            ).ToArray();

            return messageArray;
        }

       
    }
}